package gazzera.swing.browse;

import gazzera.gui.messages.*;
import gazzera.swing.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;


class FileTableColumnModel extends DefaultTableColumnModel
{
	public FileTableColumnModel(FileIconRenderer fir)
	{
		 TableColumn c = new TableColumn(0);
		 c.setHeaderValue("");
		 c.setCellRenderer(fir);
		 c.setMaxWidth(28);
		 c.setMinWidth(28);
		 c.setResizable(false);
		 addColumn(c);
		 
		 c = new TableColumn(1);
		 c.setHeaderValue("Filename");
		 addColumn(c);
		 c = new TableColumn(2);
		 c.setHeaderValue("Size");
		 addColumn(c);
	}
}

public class FileTable extends JTable
{
	TableColumnModel x = null;
	FileIconRenderer r = new FileIconRenderer();
	private JPopupMenu popupMenu = new JPopupMenu();
	private JMenuItem menuDownload;
	private final Gazzera gazzera;
	
	public FileTable(Gazzera gazzera)
	{
		this.gazzera = gazzera;
		setFillsViewportHeight(true);
		setShowGrid(false);
		setBorder(null);
		setDragEnabled(false);
		setIntercellSpacing(new Dimension(0,0));
		setCellSelectionEnabled(false);
		setColumnSelectionAllowed(false);
		setRowSelectionAllowed(true);
		setAutoCreateRowSorter(true);
		buildMenu();
		
		setColumnModel(new FileTableColumnModel(r));
		
		
		addMouseListener(new MouseAdapter(){
        	
            public void mouseClicked(MouseEvent e){
             if (e.getClickCount() == 2){
                System.out.println(" double click" );
                }
             }
            public void mousePressed(MouseEvent e) {
                showPopup(e);
              }
              public void mouseReleased(MouseEvent e) {
                showPopup(e);
              }
              private void showPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                  popupMenu(e.getComponent(), e.getX(), e.getY());
                }
              }

            } );

	}
	
    private void popupMenu(Component comp, int x, int y)
    {
    	if (this.getSelectedRowCount() > 0)
		{
    		popupMenu.show(comp, x, y);
		}
    }

	@Override
	public void setModel(TableModel dataModel)
	{
		super.setModel(dataModel);
		x = new FileTableColumnModel(r);
		JTableHeader j = getTableHeader();
		if(j!=null)
			j.setColumnModel(x);
		setColumnModel(x);
	}
	
	private void buildMenu()
	{
		menuDownload = new JMenuItem("Download"); 
		
		menuDownload.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				FileTableModel m = (FileTableModel)getModel();
				int[] rows = getSelectedRows();
				for (int i = 0; i < rows.length; i++)
				{
					RemoteFile r = m.files.get(rows[i]);
					gazzera.gui.pass(new NewDownload(r.fileID, r.name, r.size, r.fullPath, r.userHash));
				}
				//gazzera.gui.pass(new ConnectNode(getSelectedConnection().hash));
			}
		});
		
        popupMenu.add(menuDownload);
	}
	
}
